package com.ruoyi.project.business.punchhelp.controller;

import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.model.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.business.punch.service.IScheduleClockPunchService;
import com.ruoyi.project.business.punchhelp.request.FindPunchWithUserRequest;
import com.ruoyi.project.business.punchhelp.request.SaveOrUpdatePunchRequest;
import com.ruoyi.project.business.punchhelp.response.PunchWithUserResponse;
import com.ruoyi.project.system.entity.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 月排班Controller
 * 
 * @author ${author}
 * @date 2024-02-05
 */
@RestController
@RequestMapping("/business/punchhelp")
public class PunchHelpController extends BaseController {
    @Autowired
    private IScheduleClockPunchService punchService;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询打卡列表
     */
    @PostMapping("/list-of-dept")
    public TableDataInfo listOfDept(@RequestBody FindPunchWithUserRequest request) {
        startPage();
        Date yearMonth = request.getYearMonth();
        Long deptId = request.getDeptId();
        // 查指定员工的排班表
        Long userId = request.getUserId();
        Long tenantId = getTenantId();

        List<SysUser> users = userService.findUsers( deptId, userId, tenantId);
        List<PunchWithUserResponse> punches = BeanUtils.copyList(users, PunchWithUserResponse.class);

        punchService.fillPunchDatas(punches, yearMonth, tenantId);
        return getDataTable(punches);
    }

    /**
     * 代打卡
     */
    @Log(title = "代打卡", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult saveOrUpdate(@RequestBody SaveOrUpdatePunchRequest request) {
        Long tenantId = getTenantId();
        Long operatorId = getUserId();
        // TODO
        punchService.saveOrUpdate(request, operatorId, tenantId);
        return toAjax(1);
    }


    /**
     * 删除月排班
     */
    @PreAuthorize("@ss.hasPermi('business:monthschedule:remove')")
    @Log(title = "删除打卡", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(punchService.deleteScheduleClockPunchByIds(ids));
    }
}
